package com.lwq.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.lwq.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2021/6/11
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    private TokenService tokenService;

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createDate", () -> new Date(), Date.class);
        if(tokenService.getUserId() != null){
            this.strictInsertFill(metaObject, "createId", () -> tokenService.getUserId(), Long.class);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateDate", () -> new Date(), Date.class);
        if(tokenService.getUserId() != null){
            this.strictInsertFill(metaObject, "updateId", () -> tokenService.getUserId(), Long.class);
        }
    }
}
